import config from '@/common/config'
// 引入拦截器配置
import {
	requestInterceptors,
	responseInterceptors
} from './interceptors.js'
// 引入luch-request
import http from '@/uni_modules/vk-uview-ui/libs/request'
import toast from '@/uni_modules/vk-uview-ui/libs/function/toast.js'
import {
	miniProgramOauth
} from '@/common/api.js'
//  初始化请求配置
let requestConfig = {}
const initRequest = (vm) => {
	// http.setConfig((defaultConfig) => {
	// 	/* defaultConfig 为默认全局配置 */
	// 	console.log(defaultConfig)
	// 	defaultConfig.baseURL = config.baseUrl /* 根域名 */
	// 	return defaultConfig
	// })
	http.config.baseUrl = config.baseUrl
	http.config.originalData = true
	http.config.custom = {
		toast: true,
		catch: true,

	}

	http.interceptor.request = (config) => {
		console.log(config)
		requestConfig = config
		config.data = config.data || {}
		config.header.token = uni.getStorageSync('token')
		// console.log(config)
		return config;
	}
	http.interceptor.response = (response) => {
		/* 对响应成功做点什么 可使用async await 做异步操作*/
		console.log(response)
		if (response.statusCode == 401) {
			uni.clearStorageSync('token')
			uni.clearStorageSync('token_expire')
			uni.clearStorageSync('userinfo')
			if (requestConfig.url.indexOf('api/user/index') == -1) {
				uni.login({
					provider: 'weixin',
					success: async (loginRes) => {
						console.log(loginRes.code)
						const data = await miniProgramOauth({
							code: loginRes.code
						})
						console.log(data)
						if (data.register == 0) {
							uni.showToast({
								title: '请您先登录系统！',
								icon: 'none'
							})
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/tabbar/mine/mine'
								})
							}, 1000)
							return
						}
						uni.setStorageSync('token', data.token)
						uni.setStorageSync('token_expire', data.token_expire)
						uni.setStorageSync('userinfo', data.userinfo)
					}
				})
			}

			return false
		}
		const data = response.data
		// 自定义参数
		const custom = http.config?.custom
		if (data.code !== 1) { // 服务端返回的状态码不等于200，则reject()
			// 如果没有显式定义custom的toast参数为false的话，默认对报错进行toast弹出提示
			if (custom.toast !== false) {
				// toast(data.msg)
				console.log(data)
				// uni.$u.toast(data.msg)	
				uni.showToast({
					title: data.msg || '',
					icon: 'none'
				})
			}
			// 如果需要catch返回，则进行reject
			if (custom?.catch) {
				return Promise.reject(data)
			} else {
				// 否则返回一个pending中的promise
				return new Promise(() => {})
			}
		}
		return data
	}
	// requestInterceptors()
	// responseInterceptors()
}

const uploadRequest = (filePath) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${config.baseUrl}/api/common/upload`, //仅为示例，非真实的接口地址
			filePath: filePath,
			name: 'file',
			formData: {
				'Content-Type': 'multipart/form-data'
			},
			header: {
				token: uni.getStorageSync('token'),
				"Content-Type": "multipart/form-data",
			},
			success: (uploadFileRes) => {
				console.log(uploadFileRes)
				const data = JSON.parse(uploadFileRes.data)
				if (data.code == 0 && data.msg == "请登录后再进行操作") {
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/tabbar/mine/mine'
						})
					}, 1000)
				}
				resolve(data)
			}
		});
	})
}
export {
	initRequest,
	uploadRequest
}